﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Kiiro.Common.Utility;
using Kiiro.SiteDefinition.MasterPage;

namespace Kiiro.SiteDefinition.ControlTemplates
{
    public partial class CustomForm : KiiroUserControl
    {
        public NavigationSub NavigationSub
        {
            get { return ( ( KiiroMaster ) Page.Master ).NavigationSub; }
        }

        protected void Page_Load ( object sender, EventArgs e )
        {
            SPFormContext formContext = SPContext.Current.FormContext;
            string contentTypeName = SPContext.Current.List.ContentTypes [ 0 ].Name;

            if ( formContext.FormMode == SPControlMode.Edit )
            {
                LinkButton btnDelete = new LinkButton ( );
                btnDelete.Attributes.Add ( "onclick", "return confirm('Are you sure you want to delete this item')" );
                btnDelete.Attributes.Add ( "class", "icon-delete" );
                btnDelete.Text = "DELETE ITEM";
                btnDelete.Click += new EventHandler ( btnDelete_Click );
                NavigationSub.Commands.Add ( btnDelete );
            }

            if ( formContext.FormMode == SPControlMode.Display )
            {
                displayModeCommands.Visible = true;

                lnkNewItem.NavigateUrl = string.Format ( "{0}/{1}/NewForm.aspx",
                    SPContext.Current.Web.Url, SPContext.Current.List.RootFolder.Url );

                lnkEditItem.NavigateUrl = string.Format ( "{0}/{1}/EditForm.aspx?id={2}", 
                    SPContext.Current.Web.Url, SPContext.Current.List.RootFolder.Url, SPContext.Current.ListItem.ID );

                lbDeleteItem.OnClientClick = "return confirm('Are you sure you want to delete this item')";
            }

            string listHome = SPContext.Current.List.RootFolder.ServerRelativeUrl;

            if ( Request.UrlReferrer != null 
                && Request.UrlReferrer.OriginalString != null )
            {
                if ( Request.UrlReferrer.OriginalString.Contains ( listHome ) )
                {
                    lnkBack.Text = "Back To " + SPContext.Current.List.Title;
                    lnkBack.NavigateUrl = listHome;
                    pnlActionBar.Visible = true;
                }
                else if ( Request.UrlReferrer.OriginalString.Contains ( SPContext.Current.Web.Url ) )
                {
                    lnkBack.Text = "Back";
                    lnkBack.NavigateUrl = Request.UrlReferrer.OriginalString;
                    pnlActionBar.Visible = true;
                }
            }

            GoBackButton.Visible = formContext.FormMode == SPControlMode.New || formContext.FormMode == SPControlMode.Edit;
        }

        public override void RenderControl ( HtmlTextWriter writer )
        {
            base.RenderControl ( writer );
        }

        public void btnDelete_Click ( object sender, EventArgs e )
        {
            SPContext.Current.ListItem.Delete ( );
            Response.Redirect ( SPContext.Current.List.RootFolder.ServerRelativeUrl );
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/CONTROLTEMPLATES/Kiiro/CustomForm.ascx.cs $
 * 
 * 9     2/08/10 2:28p Camerons
 * 
 * 8     4/01/10 12:03p Adamw
 * 
 * 7     3/09/09 1:49p Shafia
 * Used "SPContext.Current.Web.Url" instead of
 * "SPContext.Current.Web.ServerRelativeUrl" when setting up links for new
 * item or edit item links to ensure it is setup properly if at portal
 * level as well as project level
 * 
 * 6     3/09/09 11:07a Camerons
 * FogBugz #252: Fixed NewItem url
 * 
 * 5     3/09/09 10:32a Camerons
 * FogBugz #252: Fixed EditItem url
 * 
 * 4     3/09/09 10:20a Camerons
 * FogBugz #252: Added commands for NewItem, EditItem, DeleteItem when in
 * DisplayMode
 * 
 * 3     17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/